package com.qf.Dao.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.Dao.RegisterDao;
import com.qf.entity.Register;
import com.qf.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.swing.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chengqiang
 * @Create 2023-12-05-16:26
 * Description:
 */
public class RegisterDaoImpl implements RegisterDao {
    private QueryRunner qr = DBUtils.DB.getQr();
    @Override
    public List<Register> seletByPageRegister(String rid, String dname, String startTime, String endTime, Integer currentPageNo, Integer pageSize) {
        List<Register> registerList = null;
        try {
            StringBuffer sb = new StringBuffer("select r.*,d.name as dname from register r inner join doctor d on r.did=d.did where 1=1 ");
            List resultList = new ArrayList();
            if(!StringUtils.isEmpty(rid)){
                sb.append("and r.rid=?");
                resultList.add(rid);
            }
            if(!StringUtils.isEmpty(dname)){
                sb.append(" and d.name like ?");
                resultList.add("%"+dname+"%");
            }
            if(!StringUtils.isEmpty(startTime)){
                sb.append(" and r.registerDate >?");
                resultList.add(startTime);
            }
            if(!StringUtils.isEmpty(endTime)){
                sb.append(" and r.registerDate < ?");
                resultList.add(endTime);
            }

            sb.append(" limit ?,?");
            resultList.add((currentPageNo - 1) * pageSize);
            resultList.add(pageSize);
            System.out.println(sb);
            registerList = qr.query(sb.toString(), new BeanListHandler<Register>(Register.class), resultList.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return registerList ;
    }

    @Override
    public Integer selectByRegisterCount(String rid, String dname, String startTime, String endTime) {
        Long count =null;
        StringBuffer sb = new StringBuffer("select count(1) from register r inner join doctor d on r.did=d.did where 1=1 ");
        try {
            List resultList = new ArrayList();
            if (!StringUtils.isEmpty(rid)) {
                sb.append("and r.rid=?");
                resultList.add(rid);
            }
            if (!StringUtils.isEmpty(dname)) {
                sb.append(" and d.name like ?");
                resultList.add("%" + dname + "%");
            }
            if (!StringUtils.isEmpty(startTime)) {
                sb.append(" and r.registerDate >?");
                resultList.add(startTime);
            }
            if (!StringUtils.isEmpty(endTime)) {
                sb.append(" and r.registerDate < ?");
                resultList.add(endTime);
            }

            if(resultList !=null&& resultList.size()>0){

                    count = (Long)qr.query(sb.toString(), new ScalarHandler(), resultList.toArray());

            }else {
                count = (Long) qr.query(sb.toString(),new ScalarHandler());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count==null?0:Integer.parseInt(count+"");
    }
}
